package com.sxx.beautifulcity.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sxx.beautifulcity.R;
import com.sxx.beautifulcity.dialog.CommonProgressDialog;
import com.sxx.beautifulcity.toast.commcommonToast;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.sso.UMSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;

/**
 * Created by dllo on 15/11/4.
 */
public class AboutSoftActivity extends DialogBaseActivity implements View.OnClickListener {
    //定义RelativeLayout类型的tellFriend,helpShop
    private RelativeLayout tellFriend, helpShop;
    //定义QQ平台
    private UMQQSsoHandler qqSsoHandler;
    //定义QQ空间平台
    private QZoneSsoHandler qZoneSsoHandler;
    //定义TextView
    private TextView titleTv, moreTv;
    //定义ImageView
    private ImageView backIv;

    final UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.share");
    // 添加微信平台
    private UMWXHandler wxHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about_soft);
        initView();
    }

    /***
     * 初始化
     */
    private void initView() {

        titleTv = (TextView) findViewById(R.id.titleTv);
        titleTv.setText("关于软件");
        backIv = (ImageView) findViewById(R.id.backIV);
        backIv.setOnClickListener(this);
        moreTv = (TextView) findViewById(R.id.moreTv);
        moreTv.setText("");
        //初始化
        wxHandler = new UMWXHandler(this, "wx1702d92878cf5df9",
                "d4624c36b6795d1d99dcf0547af5443d");
        wxHandler.addToSocialSDK();

        qqSsoHandler = new UMQQSsoHandler(this, "1104869349",
                "dGD1LBSMprbe54g5");
        qZoneSsoHandler = new QZoneSsoHandler(this, "1104869349",
                "dGD1LBSMprbe54g5");

        mController.setShareContent("美丽城乡,让您更健康http://www.bequtifulcity.com");
        mController.getConfig().removePlatform(SHARE_MEDIA.RENREN, SHARE_MEDIA.DOUBAN);
        tellFriend = (RelativeLayout) findViewById(R.id.tellFriend);
        tellFriend.setOnClickListener(this);
        helpShop = (RelativeLayout) findViewById(R.id.helpShop);
        helpShop.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tellFriend:
                qqShare();
                weixinShare();
                break;
            case R.id.helpShop:
                setHelpShopPopMenu(v);
                break;
            case R.id.backIV:
                Intent intent = new Intent();
                intent.setClass(this, MainActivity.class);
                startActivity(intent);
                finish();
                overridePendingTransition(R.anim.left_out, R.anim.right_into);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /**使用SSO授权必须添加如下代码 */
        UMSsoHandler ssoHandler = mController.getConfig().getSsoHandler(requestCode);
        if (ssoHandler != null) {
            ssoHandler.authorizeCallBack(requestCode, resultCode, data);
        }
    }

    /**
     * 微信分享
     */
    public void weixinShare() {
        wxHandler.addToSocialSDK();
        // 添加微信朋友圈
        UMWXHandler wxCircleHandler = new UMWXHandler(this,
                "wx1702d92878cf5df9",
                "d4624c36b6795d1d99dcf0547af5443d");
        wxCircleHandler.setToCircle(true);
        wxCircleHandler.addToSocialSDK();
    }

    /**
     * QQ分享
     */
    public void qqShare() {
        mController.openShare(this, false);
        qqSsoHandler.addToSocialSDK();
        qZoneSsoHandler.addToSocialSDK();
    }

    /***
     * 帮助商户弹出一个PopMenu
     * @param anchor
     */
    public void setHelpShopPopMenu(View anchor) {
        PopupMenu pm = new PopupMenu(this, anchor);
        // 加载菜单文件
        pm.inflate(R.menu.menu_tell_shop);

        // 设置菜单项的点击事件
        pm.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                // TODO Auto-generated method stub

                if (item.getItemId() == R.id.mergeItem) {
                    Intent intent = new Intent(AboutSoftActivity.this, JudgeSoftActivity.class);
                    startActivity(intent);
                } else if (item.getItemId() == R.id.addItem) {

                } else if (item.getItemId() == R.id.softAboutItem) {
                    final Dialog dialog = CommonProgressDialog.createLoadingDialog(
                            AboutSoftActivity.this , "正在检查软件版本,亲请耐心等候哦");
                    dialog.show();
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            dialog.dismiss();
                            commcommonToast.checkSoftVersion();
                        }
                    }, 3000);
                }
                return false;
            }
        });
        pm.show();// 显示菜单
    }
}

